package com.ttc.mBackup.BackupSMS;

import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ttc.mBackup.R;


public class BackupSMSActivity extends Activity {
	
	private ImageView imgFile, imgPhone, imgLocation, imgBackupMode;
    private TextView lblBackup, lblFilename, lblPhone, lblLocation, lblBackupMode;
    private TextView lblv_method, lblv_date, lblv_filename;
    private ImageButton btnBackup;
    ArrayList<String> phone;
    private String date;
    
    private static final String ACCOUNT = "ACCOUNT";
    private static final String FILENAME = "FILENAME";
    private static final String METHOD = "METHOD";
    private static final String DATE = "DATE";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.s_layout_backup);
				
		IntializeSource();
		restoreInformation();
		
	}

	private void IntializeSource() {
		phone = new ArrayList<String>();
		phone.add(0, "Check all");
		imgFile = (ImageView) findViewById(R.id.imgs_mainFileCreateable);
		imgPhone = (ImageView) findViewById(R.id.imgs_mainPhoneSelectable);
		imgLocation = (ImageView) findViewById(R.id.imgs_mainFileLocation);
		imgBackupMode = (ImageView) findViewById(R.id.imgs_mainBackupMode);
		
		lblBackup = (TextView) findViewById(R.id.lbls_main_backup_title);
		lblFilename = (TextView) findViewById(R.id.lbls_mainFileCreateabe);
		lblPhone = (TextView) findViewById(R.id.lbls_mainPhoneSelectable);
		lblLocation = (TextView) findViewById(R.id.lbls_mainFileLocation);
		lblBackupMode = (TextView) findViewById(R.id.lbls_mainBackupMode);
		
		lblv_date = (TextView) findViewById(R.id.lbls_table_date);
		lblv_filename = (TextView) findViewById(R.id.lbls_table_filename);
		lblv_method = (TextView) findViewById(R.id.lbls_table_method);
		
		btnBackup = (ImageButton) findViewById(R.id.btns_main_backup);
		Date time = new Date();
		date = DateFormat.format("dd.MM.yy-hh.mm.ss", time.getTime()).toString();
		lblFilename.setText("SMS-on "+ date + ".jmb");
		
		imgFile.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new CustomDialog(BackupSMSActivity.this)
							.createDialogFileSelectable(lblFilename, lblBackupMode.getText().toString());
			}
		});
		imgLocation.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new CustomDialog(BackupSMSActivity.this).createDialogLocationSelectable(lblLocation);
			}
		});
		imgBackupMode.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DialogModeSelecable();
			}
		});
		
		imgPhone.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				phone = new CustomDialog(BackupSMSActivity.this).createDialogPhoneSelectable(lblPhone);
			}
		});
		
		btnBackup.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String data = "Do you want create backup file with options?" + "\n\n" + 
							"Backup Mode: " + lblBackupMode.getText().toString() + "\n" +
							"File Name: " + lblFilename.getText().toString() + "\n" + 
							"Location: " + lblLocation.getText().toString() + "\n" +
							"With People: " + lblPhone.getText().toString();
				String location = lblLocation.getText().toString();
				String filename = lblFilename.getText().toString();
				String mode = lblBackupMode.getText().toString();
				if(filename.equals("") || filename == null){
					DialogModeSelecable();
				}else{
					CustomDialog dialog = new CustomDialog(BackupSMSActivity.this, location, phone, filename);
					dialog.setDate(date);
					dialog.createDialogComfirm(data, mode);
				}
			}
		});
	}
	
	
	private void restoreInformation(){
		SharedPreferences account = getSharedPreferences(ACCOUNT, 0);
		
		String date = account.getString(DATE, null);
		String filename = account.getString(FILENAME, null);
		String method = account.getString(METHOD, null);
		lblv_date.setText(date);
		lblv_filename.setText(filename);
		lblv_method.setText(method);
		if(date == null || date.equals("")){
			lblv_date.setText("N/A");
		}
		if(filename == null || filename.equals("")){
			lblv_filename.setText("N/A");
		}
		if(method == null || filename.equals("")){
			lblv_method.setText("N/A");
		}
	}
	
	private void DialogModeSelecable() {
		String[] choice = null;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		choice = new String[]{DialogCode.STRING_BACKUP_NEW, DialogCode.STRING_BACKUP_INCREMENTAL};
		builder.setTitle("Choose Backup Mode");
		builder.setIcon(R.drawable.s_icon_select_mode);
		final String[] temp = choice;
		builder.setSingleChoiceItems(choice, 0, null);
		
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				int itemChecked = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
				for (int i = 0; i < temp.length; i++) {
					if (itemChecked == i) {
						lblBackup.setText(temp[i]);
						if(itemChecked == 0){
							lblBackupMode.setText(DialogCode.STRING_BACKUP_NEW);
							
						}else if(itemChecked == 1){
							lblBackupMode.setText(DialogCode.STRING_BACKUP_INCREMENTAL);
							new CustomDialog(BackupSMSActivity.this).createDialogFileSelectable(lblFilename, 
										DialogCode.STRING_BACKUP_INCREMENTAL);
						}
					}
				}
			}
		});
		
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}
}
